/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.zdmya.service;

import static com.chenzehe.java.core.Status.STATUS_NORMAL;
import static com.chenzehe.java.core.Constants.OKORNOT;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenzehe.java.core.orm.Page;
import com.chenzehe.java.core.orm.PageRequest;
import com.chenzehe.zdmya.dao.ContentDao;
import com.chenzehe.zdmya.domain.Content;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-7-26 下午10:51:55
 */
@Service
@Transactional
public class ContentService extends BaseService implements IContentService {

	@Resource
	ContentDao contentDao;

	@Override
	public int saveContent(Content content) {
		contentDao.save(content);
		return 1;
	}

	@Override
	public Content getContentById(Integer id) {
		return contentDao.get(id);
	}

	@Override
	public Content getPreContentById(Integer id) {
		Content content = null;
		int nid = id;
		while (nid > 0
				&& (content == null || content.getStatus() != STATUS_NORMAL
						.getState())) {
			nid -= 1;
			content = contentDao.get(nid);
		}
		return content;
	}

	@Override
	public Content getNextContentById(Integer id) {
		Content content = null;
		int nid = id;
		int idNum = 10;
		while ((id + idNum) > nid
				&& (content == null || content.getStatus() != STATUS_NORMAL
						.getState())) {
			nid += 1;
			content = contentDao.get(nid);
		}
		return content;
	}

	@Override
	public Page<Content> queryPageContent(PageRequest pageRequest) {
		String hql = "FROM Content x WHERE status=? AND is_toped=?";
		Page<Content> pageContent = contentDao.findPage(pageRequest, hql,
				STATUS_NORMAL.getState(), OKORNOT.IS_NOT.getState());
		return pageContent;
	}

	@Override
	public Page<Content> queryByCategory(PageRequest pageRequest,
			String pename, String ename) {
		String cateName = null;
		StringBuilder hqlBuilder = new StringBuilder("FROM Content x ");
		if (StringUtils.isNotBlank(ename)) {
			hqlBuilder.append("WHERE categoryEname=? AND status=?");
			cateName = ename;
		} else {
			hqlBuilder.append("WHERE categoryParentEname=? AND status=?");
			cateName = pename;
		}

		Page<Content> categoryContent = contentDao.findPage(pageRequest,
				hqlBuilder.toString(), cateName, STATUS_NORMAL.getState());
		return categoryContent;
	}

	@Override
	public Page<Content> queryByType(PageRequest pageRequest, String ename) {
		String hql = "FROM Content x WHERE typeEname=? AND status=?";
		Page<Content> discountContent = contentDao.findPage(pageRequest, hql,
				ename, STATUS_NORMAL.getState());
		return discountContent;
	}

	@Override
	public Page<Content> queryByArea(PageRequest pageRequest, String ename) {
		String hql = "FROM Content x WHERE areaEname=? AND status=?";
		Page<Content> discountContent = contentDao.findPage(pageRequest, hql,
				ename, STATUS_NORMAL.getState());
		return discountContent;
	}

	@Override
	public Page<Content> queryByMall(PageRequest pageRequest, String ename) {
		String hql = "FROM Content x WHERE mallEname=? AND status=?";
		Page<Content> siteContent = contentDao.findPage(pageRequest, hql,
				ename, STATUS_NORMAL.getState());
		return siteContent;
	}

	@Override
	public List<Content> queryTopContent() {
		String hql = "FROM Content x WHERE status=? AND is_toped=?";
		List<Content> topContent = contentDao.find(hql, STATUS_NORMAL.getState(),
				OKORNOT.IS_OK.getState());
		return topContent;
	}

	@Override
	public List<Content> queryLimitContent(int limit) {
		String hql = "FROM Content WHERE status=? ORDER BY id DESC";
		return contentDao.findLimit(hql, limit, STATUS_NORMAL.getState());
	}

	@Override
	public List<Content> queryLimitContentByCategoryEname(int limit,
			String categoryEname) {
		String hql = "FROM Content WHERE categoryEname=? AND status=? ORDER BY id DESC";
		return contentDao.findLimit(hql, limit, categoryEname, STATUS_NORMAL.getState());
	}

	@Override
	public Content getById(int id) {
		String hql = "FROM Content WHERE id=? AND status=?";
		List<Content> listContents = contentDao.find(hql, id, STATUS_NORMAL.getState());
		if (listContents != null && listContents.size() > 0) {
			return listContents.get(0);
		}
		return null;
	}

	@Override
	public int updateCommentCount(int contentId) {
		String hql = "UPDATE Content SET commentCount=commentCount+1 WHERE id=?";
		return contentDao.batchExecute(hql, contentId);
	}

	@Override
	public int updateCollectionCount(int contentId) {
		String hql = "UPDATE Content SET collectionCount=collectionCount+1 WHERE id=?";
		return contentDao.batchExecute(hql, contentId);
	}

	@Override
	public int updatePingFen(int contentId, int rate) {
		String setValue = null;
		if (rate == 1) {
			setValue = "zhiCount=zhiCount+1";
		} else {
			setValue = "nozhiCount=nozhiCount+1";
		}
		String hql = "UPDATE Content SET " + setValue + " WHERE id=?";
		return contentDao.batchExecute(hql, contentId);
	}

	@Override
	public String getGoUrl(int id) {
		String hql = "SELECT new Content(goUrl) FROM Content WHERE id=?";
		List<Content> listContent = contentDao.find(hql, id);
		if (listContent != null && listContent.size() > 0) {
			return listContent.get(0).getGoUrl();
		}
		return null;
	}

	@Override
	public Long queryNumByUser(int userId) {
		String sql = "SELECT COUNT(*) FROM tbl_content WHERE user_id=? AND status=?";
		return jdbcTemplate.queryForLong(sql, userId, STATUS_NORMAL.getState());
	}
}
